//
//  WH_PersonalViewController.m
//  App_6.23
//
//  Created by wanghao on 16/8/16.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_PersonalViewController.h"
#import "WH_orderAViewController.h"
#import "WH_LuckViewController.h"
#import "WH_ShareListViewController.h"

@interface WH_PersonalViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *selectView;
@property (nonatomic, strong) UIButton *beforeButton;
@property (nonatomic, strong) UIScrollView *backScroll;//

@property (nonatomic, strong) WH_orderAViewController *orderAVC;
@property (nonatomic, strong) WH_LuckViewController *luckVC;
@property (nonatomic, strong) WH_ShareListViewController *shareVC;


@end

@implementation WH_PersonalViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
    [IQKeyboardManager sharedManager].enable = NO;
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.tabBarController.tabBar setHidden:NO];
    [IQKeyboardManager sharedManager].enable = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Person Center";
    [self createHV];
    [self createButton];
    [self createScroll];
}

- (void)createScroll{
    
    //创建scrollView
    self.backScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 150, SCREEN_WIDTH, SCREEN_HEIGHT - 150)];
    self.backScroll.pagingEnabled = YES;
    self.backScroll.bounces = NO;
    self.backScroll.showsHorizontalScrollIndicator = NO;
    self.backScroll.delegate = self;
    self.backScroll.contentSize = CGSizeMake(SCREEN_WIDTH*3, 0);
    [self.view addSubview:self.backScroll];
    
    //创建独立controller
    self.shareVC = [[WH_ShareListViewController alloc] init];
    _shareVC.shareID = _personID;
    self.shareVC.view.frame = CGRectMake(SCREEN_WIDTH*2, 0, SCREEN_WIDTH, self.backScroll.frame.size.height);
    self.luckVC = [[WH_LuckViewController alloc] init];
    _luckVC.luckID = _personID;
    self.luckVC.view.frame = CGRectMake(SCREEN_WIDTH*1, 0, SCREEN_WIDTH, self.backScroll.frame.size.height);
    self.orderAVC = [[WH_orderAViewController alloc] init];
    _orderAVC.orderPersonID = _personID;
    self.orderAVC.view.frame = CGRectMake(SCREEN_WIDTH*0, 0, SCREEN_WIDTH, self.backScroll.frame.size.height);
    //将独立controller的View加到scrollview上
    [self addChildViewController:self.orderAVC];
    [self addChildViewController:self.luckVC];
    [self addChildViewController:self.shareVC];
    
    [self.backScroll addSubview:self.orderAVC.view];
    [self.backScroll addSubview:self.luckVC.view];
    [self.backScroll addSubview:self.shareVC.view];
    
}
- (void)createHV{
    
    UIImageView *topImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    topImg.backgroundColor = [UIColor colorWithRed:0.96 green:0.33 blue:0.41 alpha:1];
    [self.view addSubview:topImg];
    
    UIImageView *iconImg = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 60, 60)];
    [iconImg sd_setImageWithURL:[NSURL URLWithString:_personImg] placeholderImage:PLACEHOLDER_CELL_SAME];
    iconImg.layer.cornerRadius = 30;
    iconImg.layer.masksToBounds = YES;
    [topImg addSubview:iconImg];
    
    UILabel *nickLabel = [[UILabel alloc] initWithFrame:CGRectMake(iconImg.frame.origin.x + iconImg.frame.size.width + 10, iconImg.frame.origin.y, SCREEN_WIDTH - iconImg.frame.origin.x - iconImg.frame.size.width - 20, 30)];
    nickLabel.text = _personName;
    nickLabel.textColor = [UIColor whiteColor];
    [topImg addSubview:nickLabel];
    
     UILabel *idLabel = [[UILabel alloc] initWithFrame:CGRectMake(nickLabel.frame.origin.x, nickLabel.frame.origin.y + nickLabel.frame.size.height + 5, nickLabel.frame.size.width, nickLabel.frame.size.height)];
    idLabel.text = [NSString stringWithFormat:@"ID : %@",_personID];
    idLabel.textColor = [UIColor whiteColor];
    [topImg addSubview:idLabel];
}
-(void)createButton{
    
    NSArray *btnArr = [NSArray arrayWithObjects:@"夺宝记录", @"幸运记录", @"晒单分享", nil];
    
    for (int i = 0; i < btnArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:17];
        [button setTitle:btnArr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.tag = 4000 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(@100);
            make.left.mas_equalTo(@(SCREEN_WIDTH / 3 * i));
            make.width.mas_equalTo(@(SCREEN_WIDTH / 3));
            make.height.mas_equalTo(@50);
        }];
    }
    
    UILabel *xiLabel = [UILabel new];
    xiLabel.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1];
    [self.view addSubview:xiLabel];
    [xiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(@149);
        make.left.right.mas_equalTo(@0);
        make.height.mas_equalTo(@1);
        
    }];
    self.selectView = [[UIView alloc] init];
    self.selectView.backgroundColor = [UIColor colorWithRed:0.82 green:0.13 blue:0.3 alpha:1];
    [self.view addSubview:self.selectView];
    
    [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(@148);
        make.left.mas_equalTo(@0);
        make.width.mas_equalTo(@(SCREEN_WIDTH / 3));
        make.height.mas_equalTo(@2);
    }];
    
    UIButton * button = (UIButton *)[self.view viewWithTag:4000];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
}

-(void)buttonAction:(UIButton *)button{
    
    
    if (button.tag != 4000) {
        
        UIButton * buttonA = (UIButton *)[self.view viewWithTag:4000];
        [buttonA setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    }
    
    if (![button isEqual:self.beforeButton]) {
        
        [self.beforeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        CGFloat x = (button.tag - 4000) * (SCREEN_WIDTH / 3);
        
        [self.selectView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(@(x));
        }];
        
        [UIView animateWithDuration:0.2 animations:^{
            
            [self.view layoutIfNeeded];
            
        }];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        self.beforeButton = button;
        
    }
    self.backScroll.contentOffset = CGPointMake(SCREEN_WIDTH*(button.tag - 4000), 0);
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    

    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    
    UIButton *button = (UIButton *)[self.view viewWithTag:4000 + index];
    
    if (button.tag != 4000) {
        
        UIButton * buttonA = (UIButton *)[self.view viewWithTag:4000];
        [buttonA setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    }

    if (![button isEqual:self.beforeButton]) {
        
        [self.beforeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        CGFloat x = (button.tag - 4000) * (SCREEN_WIDTH / 3);
        
        [self.selectView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(@(x));
        }];
        
        [UIView animateWithDuration:0.2 animations:^{
            
            [self.view layoutIfNeeded];
            
        }];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        self.beforeButton = button;
        
    }

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
